package com.code.app.floatview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

import com.code.app.R;

public class FloatCircleView extends View {

    public int width;

    public int height;

    private Paint circlePaint;

    private Paint textPaint;

    private boolean drag;

    private Bitmap bitmap;

    private String currentText = "50%";

    private float dp2px(int dp) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
    }

    private float sp2px(int sp) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, getResources().getDisplayMetrics());
    }

    public FloatCircleView(Context context) {
        super(context);
        init();
    }

    public FloatCircleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public FloatCircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setDrag(boolean drag) {
        this.drag = drag;
        invalidate();
    }

    private void init() {
        circlePaint = new Paint();
        circlePaint.setAntiAlias(true);
        circlePaint.setColor(Color.GRAY);

        textPaint = new Paint();
        textPaint.setAntiAlias(true);
        textPaint.setColor(Color.WHITE);
        textPaint.setTextSize(sp2px(15));
        //文字加粗
        textPaint.setFakeBoldText(true);

        width = height = (int) dp2px(100);

        Bitmap src = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_round);
        bitmap = Bitmap.createScaledBitmap(src, width, height, true);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (drag) {
            canvas.drawBitmap(bitmap, 0, 0, null);
        } else {
            canvas.drawCircle(width / 2, height / 2, height / 2, circlePaint);

        /*Rect rect = new Rect();
        textPaint.getTextBounds(currentText, 0, currentText.length(), rect);
        int dx = width / 2 - rect.width() / 2;*/

            int dx = (int) (width / 2 - textPaint.measureText(currentText) / 2);

            Paint.FontMetricsInt fontMetricsInt = textPaint.getFontMetricsInt();
            int dy = (fontMetricsInt.bottom - fontMetricsInt.top) / 2 - fontMetricsInt.bottom;
            int baseLine = height / 2 + dy;
            canvas.drawText(currentText, dx, baseLine, textPaint);
        }
    }


}
